use rusqlite::{Connection, Result};

#[derive(Debug)]
struct Config{
    id:i32,
    proxy:String,
    worker_num:i32,
    combine_type:i32,
}
fn main() -> Result<()> {
    // 连接数据库（自动创建文件）
    let conn = Connection::open("local.db")?;
    
    // 创建表
    conn.execute(
        "CREATE TABLE IF NOT EXISTS global_config (
            id INTEGER PRIMARY KEY,
            proxy TEXT,
            worker_num INTEGER,
            combine_type INTEGER
        )", []
    )?;
    
    // 插入数据
    conn.execute(
        "INSERT INTO global_config (proxy, worker_num, combine_type) VALUES (?1, ?2, ?3)",
        ("http://127.0.0.1:1081", 27, 2)
    )?;
    
    // 查询数据
    let mut stmt = conn.prepare("SELECT id, proxy, worker_num, combine_type FROM global_config")?;
    let rows = stmt.query_map([], |row| {
        Ok(Config{
            id: row.get::<_, i32>(0)?,
            proxy: row.get::<_, String>(1)?,
            worker_num: row.get::<_, i32>(2)?,
            combine_type: row.get::<_, i32>(3)?
        })
    })?;
    
    for row in rows {
        let config = row?;
        println!("global_config: {config:?}");
    }
    Ok(())
}
#[test]
pub fn test() -> Result<()>{
    main()
}